package com.atguigu.bigdata.spark.core.rdd.persist

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

/**
  * create by undeRdoG on  2021-06-14  14:07
  * 凡心所向，素履以往，生如逆旅，一苇以航。
  */
object Spark01_RDD_Persist {
  def main(args: Array[String]): Unit = {
    val sparkConf = new SparkConf().setMaster("local").setAppName("WordCount")
    val sc = new SparkContext(sparkConf)

    val list = List("Hello Scala","Hello Spark")

    val rdd: RDD[String] = sc.makeRDD(list)

    val flatRDD = rdd.flatMap(_.split(" "))

    val mapRDD = flatRDD.map(x => (x,1))

    val res: RDD[(String, Int)] = mapRDD.reduceByKey(_ + _)

    res.collect().foreach(println)


    val list1 = List("Hello Scala","Hello Spark")

    val rdd1: RDD[String] = sc.makeRDD(list1)

    val flatRDD1 = rdd1.flatMap(_.split(" "))

    val mapRDD1 = flatRDD1.map(x => (x,1))

    val res1: RDD[(String, Iterable[Int])] = mapRDD1.groupByKey()

    res1.collect().foreach(println)
  }
}
